package me.leiusual.sharing.ui.order

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.OrderLogistics
import me.leiusual.sharing.repository.OrderRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-08-11
 * description:
 */
class OrderLogisticsViewModel @Inject constructor(
    private val orderRepository: OrderRepository
) : ViewModel() {

  // 查询物流信息

  private val _orderLogistics = MutableLiveData<ParamsOrderLogistics>()
  val orderLogistics: LiveData<Data<List<OrderLogistics.Response.RealLogistics.Data>>> = switchMap(_orderLogistics) {
    orderRepository.orderLogistics(it.number, it.type).toLiveData()
  }

  fun orderLogistics(number: String, type: String) {
    _orderLogistics.value = ParamsOrderLogistics(number, type)
  }

  data class ParamsOrderLogistics(val number: String, val type: String)
}